package de.jmda.home.bl.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email
{
	private final static String USERNAME = "rowe.jmda@gmail.com";
	private final static String PASSWORD = "hanssarpei";
	private final static Properties PROPERTIES = new Properties();
	
	static
	{
		PROPERTIES.put("mail.smtp.auth", "true");
		PROPERTIES.put("mail.smtp.starttls.enable", "true");
		PROPERTIES.put("mail.smtp.host", "smtp.gmail.com");
		PROPERTIES.put("mail.smtp.port", "587");
	}

	public static void send(String recipient, String subject, String text)
			throws AddressException, MessagingException
	{
		Session session = Session.getInstance(PROPERTIES, new javax.mail.Authenticator()
		{
			protected PasswordAuthentication getPasswordAuthentication()
			{
				return new PasswordAuthentication(USERNAME, PASSWORD);
			}
		});

		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(USERNAME));
		message.setRecipients(Message.RecipientType.TO,
		    InternetAddress.parse("rog.weg@gmail.com"));
		message.setSubject("mail from " + USERNAME);
		message.setText(text);

		Transport.send(message);
	}

	public static void main(String[] args) throws AddressException, MessagingException
	{
		send("rog.weg@gmail.com", "subject", "text");
	}
}